﻿/*
 * (c) Stefan Bojarovski 2010
 * */
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASTBuilder
{
    class PrintNode : NodesCollection, ITreeNode
    {
        private LexemeType lexType = LexemeType.PRINT;
        private ITreeNode Argument; //Expression or Identifier

        public PrintNode(ITreeNode someExpr)
        {
            Argument = someExpr;
        }
        public LexemeType getLexemeType()
        {
            return lexType;
        }
        public void printNode(String parentLevel, StreamWriter someStream)
        {
            someStream.WriteLine(parentLevel + " " + "Print: ");
            Argument.printNode(parentLevel + " ", someStream);
        }
        public List<Command> evalNode(ref Direction next, 
                                        ref Dictionary<String, Pair<IdentifierType,object>>Environment)
        {
            List<Command> printList = new List<Command>();
            if (next != Direction.JUMP)
            {
                Command printArg = Argument.evalNode(ref next, ref Environment)[0];

                if (printArg.getCommandType() == CommandType.EVALUABLE)
                {
                    printList.Add(new Command(CommandType.OUTPUT, printArg.getIdName(), printArg.getIdValue()));
                }
                else
                {
                    printList.Add(printArg);
                }
                return printList;
            }
            else
            {
                printList.Add(new Command(CommandType.VOID, null, null));
                return printList;
            }
        }
    }
}
